package com.sanyuan.cloud.module.system.controller.app.position;

import com.sanyuan.cloud.framework.apilog.core.annotation.ApiAccessLog;
import com.sanyuan.cloud.framework.common.pojo.CommonResult;
import com.sanyuan.cloud.framework.common.pojo.PageResult;
import com.sanyuan.cloud.module.system.annotation.AddGroup;
import com.sanyuan.cloud.module.system.annotation.QueryGroup;
import com.sanyuan.cloud.module.system.annotation.UpdateGroup;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.req.PositionAssignReq;
import com.sanyuan.cloud.module.system.controller.app.position.req.CheckRecordReqVO;
import com.sanyuan.cloud.module.system.controller.app.position.req.OverViewStatisticsReqVO;
import com.sanyuan.cloud.module.system.controller.app.position.req.PositionReqVO;
import com.sanyuan.cloud.module.system.controller.app.position.req.VisitReqVO;
import com.sanyuan.cloud.module.system.controller.app.position.vo.*;
import com.sanyuan.cloud.module.system.service.nongmo.AppPositionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.sanyuan.cloud.framework.common.pojo.CommonResult.success;

/**
 * @Description: 小程序端标点管理
 * @Author: wuyuwei
 * @Date: 2025/3/28
 */
@Tag(name = "小程序-标点管理")
@RequestMapping("/system/position")
@RestController
public class AppPositionController {

    @Resource
    private AppPositionService appPositionService;

    /**
     * 添加标点信息
     * @param reqVO
     * @return
     */
    @PostMapping("/add")
    @ApiAccessLog
    public CommonResult<Boolean> addPosition(@RequestBody @Validated(AddGroup.class) PositionReqVO reqVO){
        return success(appPositionService.addPosition(reqVO));
    }

    /**
     * 查询给定经纬度附附近的位置信息
     * @param reqVO
     * @return
     */
    @Operation(summary = "查询给定经纬度附附近的位置信息")
    @ApiAccessLog
    @PostMapping("/nearby")
    public CommonResult<List<SystemPositionV0>> getNearby(@RequestBody @Validated(QueryGroup.class) PositionReqVO reqVO) {
        List<SystemPositionV0> positions = appPositionService.findNearbyPositions(reqVO.getLongitude(), reqVO.getLatitude(), reqVO.getRadiusKm());
        return success(positions);
    }


    /**
     * 标点指派
     * @param req
     * @return
     */
    @Operation(summary = "标点指派")
    @ApiAccessLog
    @PostMapping("/assign")
    public CommonResult<Boolean> assign(@RequestBody @Validated(UpdateGroup.class) PositionAssignReq req){
        appPositionService.assign(req);
        return success(true);
    }

    /**
     * 标点删除
     * @param id
     * @return
     */
    @Operation(summary = "标点删除")
    @ApiAccessLog
    @GetMapping("/delete")
    public CommonResult<Boolean> delete(@RequestParam("id") String id){
        return success(appPositionService.delete(id));
    }

    /**
     * 查询标点详情
     * @param id
     * @return
     */
    @Operation(summary = "查询标点详情")
    @ApiAccessLog
    @GetMapping("/detail")
    public CommonResult<PositionVO> detail(@RequestParam("id") String id){
        return success(appPositionService.detail(id));
    }


    @Operation(summary = "查询时间段内用户拜访情况")
    @ApiAccessLog
    @PostMapping("/getVisitInfoByUserAndTimeSlot")
    public CommonResult<UserVisitInfoVO> getVisitInfoByUserAndTimeSlot(@RequestBody @Validated(QueryGroup.class) VisitReqVO reqVO){
        return success(appPositionService.getVisitInfoByUserAndTimeSlot(reqVO));
    }


    @Operation(summary = "查询某个区域内拜访情况")
    @ApiAccessLog
    @PostMapping("/getDistrictVisitInfo")
    public CommonResult<List<DistrictVisitInfoVO>> getDistrictVisitInfo(@RequestBody PositionReqVO reqVO) {
        return success(appPositionService.getDistrictVisitInfo(reqVO));
    }

    /**
     * 出发申请
     * @param reqVO 打卡地点信息
     * @return 初始化结果
     */
    @Operation(summary = "出发申请")
    @ApiAccessLog
    @PostMapping("/initCheckInLocation")
    public CommonResult<Boolean> initCheckInLocation(@RequestBody @Validated(QueryGroup.class) PositionReqVO reqVO) {
        return success(appPositionService.initCheckInLocation(reqVO));
    }


    @Operation(summary = "打卡")
    @ApiAccessLog
    @PostMapping("/checkIn")
    public CommonResult<Boolean> checkIn(@RequestBody @Validated(QueryGroup.class) PositionReqVO reqVO) {
        return success(appPositionService.checkIn(reqVO));
    }

    @Operation(summary = "修改拜访状态")
    @ApiAccessLog
    @PostMapping("/updateVisitStatus")
    public CommonResult<Boolean> updateVisitStatus(@RequestBody @Validated(UpdateGroup.class) VisitReqVO reqVO) {
        return success(appPositionService.updateVisitStatus(reqVO));
    }

    @Operation(summary = "总览统计")
    @ApiAccessLog
    @PostMapping("/overViewStatistics")
    public CommonResult<OverViewStatisticsRespVO> overViewStatistics(@RequestBody OverViewStatisticsReqVO reqVO) {
        return success(appPositionService.overViewStatistics(reqVO));
    }

    @Operation(summary = "查询业务员标点拜访打卡记录")
    @ApiAccessLog
    @PostMapping("/getCheckRecord")
    public CommonResult<PageResult<CheckRecordRespVO>> getCheckRecord(@RequestBody CheckRecordReqVO reqVO) {
        return success(appPositionService.getCheckRecord(reqVO));
    }

    @Operation(summary = "查询业务员最近的一条标点数据")
    @ApiAccessLog
    @PostMapping("/getLatestRecord")
    public CommonResult<PositionVO> getLatestRecord(@RequestBody VisitReqVO reqVO) {
        return success(appPositionService.getLatestRecord(reqVO));
    }
}
